package com.meida.module.bus.provider.controller;

import cn.hutool.core.bean.BeanUtil;
import com.google.common.collect.Maps;
import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.security.OpenHelper;
import com.meida.common.springmvc.base.BaseController;
import com.meida.common.utils.FlymeUtils;
import com.meida.common.utils.StringUtils;
import com.meida.module.bus.client.entity.BusProject;
import com.meida.module.bus.client.entity.srmapi.basedata.SrmProjectinfo;
import com.meida.module.bus.provider.api.util.PmsApiUtil;
import com.meida.module.bus.provider.service.BusProjectService;
import com.meida.module.bus.provider.service.srmservice.SrmSqlserCommonService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Collection;
import java.util.List;
import java.util.Map;


/**
 * 项目表控制器
 *
 * @author flyme
 * @date 2020-09-25
 */
@RestController
@RequestMapping("/bus/project/")
public class BusProjectController extends BaseController<BusProjectService, BusProject> {

    @Autowired
    private BusProjectService service;
    @Autowired
    private SrmSqlserCommonService srmSqlserCommonService;
    @Autowired
    private PmsApiUtil pmsApiUtil;
    /**
     * 查询所有项目以及版本和创建人并分页
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "项目表-分页列表", notes = "项目表分页列表")
    @GetMapping(value = "page")
    public ResultBody pageList(@RequestParam(required = false) Map params) {
        return bizService.pageList(params);
    }

    /**
     * 查询所有项目以及版本和创建人并分页
     *
     * @param params
     * @return
     */
    @ApiOperation(value = "材料申请列表", notes = "材料申请列表")
    @GetMapping(value = "PurchasePage")
    public ResultBody pageList1(@RequestParam(required = false) Map params) {
        return service.purchasePageList(params);
    }


    @ApiOperation(value = "项目表-列表", notes = "项目表列表")
    @GetMapping(value = "list")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return bizService.listEntityMap(params);
    }

    @ApiOperation(value = "项目表-添加", notes = "添加项目表")
    @PostMapping(value = "save")
    public ResultBody save(@RequestParam(required = false) Map params) {
        Long userId = OpenHelper.getUserId();
        params.put("userId", userId);
        params.put("versionId", 100.00);
        return bizService.add(params);
    }

    @ApiOperation(value = "项目表-更新", notes = "更新项目表")
    @PostMapping(value = "update")
    public ResultBody update(@RequestParam(required = false) Map params) {
        return bizService.edit(params);
    }

    @ApiOperation(value = "项目表-删除", notes = "删除项目表")
    @PostMapping(value = "remove")
    public ResultBody remove(@RequestParam(required = false) Map params) {
        return bizService.delete(params);
    }


    @ApiOperation(value = "项目表-详情", notes = "项目表详情")
    @GetMapping(value = "get")
    public ResultBody get(@RequestParam(required = false) Map params) {
        return bizService.get(params);
    }

    @ApiOperation(value = "更新项目表", notes = "更新项目表")
    @PostMapping(value = "setState")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "ids", required = true, value = "主键", paramType = "form"),
        @ApiImplicitParam(name = "state", required = true, value = "状态值", paramType = "form")
    })
    public ResultBody setState(@RequestParam(required = false) Map map, @RequestParam(value = "state") Integer state) {
        return bizService.setState(map, "state", state);
    }

    /**
     * 导出项目表统计
     *
     * @param params
     * @param request
     * @param response
     */
    @PostMapping(value = "export")
    public void export(@RequestParam(required = false) Map params, HttpServletRequest request, HttpServletResponse response) {
        bizService.export(params, request, response, "项目表统计", "项目表统计表","busProjectExportHandler");
    }

    @ApiOperation(value = "用户分配项目", notes = "用户分配项目")
    @PostMapping("/setProject")
    public ResultBody addUserProjects(
        @RequestParam(value = "userId") Long userId,
        @RequestParam(value = "projectIds", required = false) String projectIds
    ) {
        bizService.saveUserProjects(userId, StringUtils.isNotBlank(projectIds) ? projectIds.split(",") : new String[]{});
        return ResultBody.msg("设置成功");
    }
    @ApiOperation(value = "分配项目部门权限", notes = "分配项目部门权限")
    @PostMapping("/setprojectdept")
    public ResultBody setprojectdept(
        @RequestParam(value = "projectId") Long projectId,
        @RequestParam(value = "deptIds", required = false) String deptIds
    ) {
        bizService.saveDeptProjects(projectId, StringUtils.isNotBlank(deptIds) ? deptIds.split(",") : new String[]{});
        return ResultBody.msg("设置成功");
    }
    @ApiOperation(value = "获取分配项目列表")
    @GetMapping(value = "getUserProjectList")
    public ResultBody getUserProjectList(@RequestParam(value = "userId") Long userId) {
        return bizService.getUserProjectList(userId);
    }
    @ApiOperation(value = "获取分配部门")
    @GetMapping(value = "getdeptList")
    public ResultBody getdeptProjectList(@RequestParam(value = "projectId") Long projectId) {
        return bizService.getDeptProjects(projectId);
    }

    @ApiOperation(value = "获取项目当前状态")
    @GetMapping(value = "getprojectStatus")
    public ResultBody getprojectStatus(@RequestParam(value = "projectId") Long projectId) {
        if(FlymeUtils.isEmpty(projectId)){
            return ResultBody.failed("项目id为空");
        }
        BusProject busProject= bizService.getById(projectId);
        return ResultBody.ok(busProject.getState());
    }
    @ApiOperation(value = "获取项目是否转存100")
    @GetMapping(value = "getprojectVersion100")
    public ResultBody getprojectVersion100(@RequestParam(value = "projectId") Long projectId) {
        if(FlymeUtils.isEmpty(projectId)){
            return ResultBody.failed("项目id为空");
        }
        BusProject busProject= bizService.getById(projectId);
        return ResultBody.ok(busProject.getVersion100());
    }

    @ApiOperation(value = "对外月报状态提交oa审核", notes = "对外月报状态提交oa审核")
    @PostMapping("/setReportOutStateCheck")
    public ResultBody setReportOutStateCheck(@RequestParam(value = "projectId") Long projectId) {
        return bizService.setReportOutStateCheck(projectId);
    }

    @ApiOperation(value = "获取SRM项目信息", notes = "获取SRM项目信息")
    @GetMapping("/getSrmProject")
    public ResultBody getSrmProject() {
        List<SrmProjectinfo> srmProjectinfos = srmSqlserCommonService.getSrmProjectIno(Maps.newHashMap());
        return ResultBody.ok(srmProjectinfos);
    }

    @ApiOperation(value = "同步SRM项目信息到PMS", notes = "同步SRM项目信息到PMS")
    @GetMapping("/syncSrmProject")
    public ResultBody syncSrmProject(@RequestParam(value = "projectCode") String projectCode) {
        Map<String, Object> searchMap = Maps.newHashMap();
        searchMap.put("projectCode", projectCode);
        List<SrmProjectinfo> srmProjectinfos = srmSqlserCommonService.getSrmProjectIno(searchMap);
        Collection<BusProject> busProjects = bizService.listByMap(searchMap);
        if(0 < busProjects.size() && 0 < srmProjectinfos.size()) {
            //新加进来的项目
            SrmProjectinfo srmProjectinfo = srmProjectinfos.get(0);
            BusProject busProject = busProjects.stream().findFirst().get();
            setSyncProjectData(busProject, srmProjectinfo);
            bizService.saveOrUpdate(busProject);
        } else if(0 < srmProjectinfos.size()) {
            //已存在的项目
            SrmProjectinfo srmProjectinfo = srmProjectinfos.get(0);
            BusProject busProject = new BusProject();
            BeanUtil.copyProperties(searchMap, busProject);
            busProject.setProjectId(srmProjectinfo.getProjectId());
            busProject.setProjectCode(srmProjectinfo.getProjectCode());
            setSyncProjectData(busProject, srmProjectinfo);
            bizService.save(busProject);
        }
        return ResultBody.ok();
    }

    /**
     * 设置同步过来的项目信息
     * @param busProject
     * @param srmProjectinfo
     */
    public void setSyncProjectData(BusProject busProject, SrmProjectinfo srmProjectinfo) {
        busProject.setProjectName(srmProjectinfo.getProjectName());
        busProject.setProjectAddress(srmProjectinfo.getProjectAddress());
        busProject.setProjectCgg(srmProjectinfo.getBusinessName()); // 项目采购岗
        busProject.setProjectSwg(srmProjectinfo.getBusinessName()); // 项目商务岗
        busProject.setProjectCwg(srmProjectinfo.getSafetyName()); // 项目账务
        busProject.setProjectXm(srmProjectinfo.getManagerName()); // 项目经理
        busProject.setSuppliesCg(srmProjectinfo.getGoodsPurGroupName()); // 物资采购
        busProject.setBusinessCg(srmProjectinfo.getBusinessPurGroupName()); // 商务岗采购
        busProject.setProjectLinkMan(srmProjectinfo.getProjectContacts()); // 项目联系人
        busProject.setLinkTel(srmProjectinfo.getPhone1()); // 项目人电话
        busProject.setLinkEmail(srmProjectinfo.getEmail1()); // 项目联系人
        busProject.setMaterialsLinkMan(srmProjectinfo.getMaterialClerk()); // 材料员
        busProject.setMaterialsTel(srmProjectinfo.getPhone2()); // 材料员电话
        busProject.setMaterialsEmail(srmProjectinfo.getEmail2()); // 材料员邮箱
        busProject.setDeliveryLinkMan(srmProjectinfo.getReceivingClerk()); // 收货员名称
        busProject.setDeliveryTel(srmProjectinfo.getPhone3()); // 收货员电话
        busProject.setDeliveryEmai(srmProjectinfo.getEmail3()); // 收货员邮箱
        busProject.setState(0);//解锁状态
        busProject.setBusinessPurGroupCode(srmProjectinfo.getBusinessPurGroupCode());//采购组
        busProject.setBusinessPurGroupName(srmProjectinfo.getBusinessPurGroupName());
    }

    @ApiOperation(value = "合同是否下达CHECK", notes = "合同是否下达CHECK")
    @GetMapping(value = "srmContractsCK")
    public ResultBody srmContractsCK(Long projectId, String packageNo) {
        return pmsApiUtil.ckSrmContractStatus(projectId, packageNo);
    }

}